package com.guli.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 使用lamdar表达式
     * @return
     */
    @Override
    public List<CategoryEntity> treeList() {
        List<CategoryEntity> allList = baseMapper.selectList(null);
        List<CategoryEntity> collect = allList.stream().filter(item -> {
            return item.getParentCid().equals(0L);
        }).map(l1 -> {
            List<CategoryEntity> chirldrens = chirldrens(allList, l1);
            l1.setChildren(chirldrens);
            return l1;
        }).sorted((s1,s2) -> {
            return (s1.getSort()!=null?0:s1.getSort()) - (s2.getSort()!=null?0:s2.getSort());
        }).collect(Collectors.toList());
        return collect;
    }
    private List<CategoryEntity> chirldrens(List<CategoryEntity> allList,CategoryEntity cate) {
        List<CategoryEntity> collect = allList.stream().filter(item -> {
            return item.getParentCid().equals(cate.getCatId());
        }).sorted((s1,s2) -> {
            return (s1.getSort()!=null?0:s1.getSort()) - (s2.getSort()!=null?0:s2.getSort());
        }).map(c -> {
            List<CategoryEntity> chirldrens = chirldrens(allList, c);
            c.setChildren(chirldrens);
            return c;
        }).collect(Collectors.toList());
        return collect;
    }
    /**
     * 9.后台三级分类树的实现（lamdar表达式的使用）
     * @return
     */
  /*  @Override
    public List<CategoryEntity> treeList() {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_cid",0);
        List<CategoryEntity> categoryEntities = baseMapper.selectList(wrapper);
        for (CategoryEntity categoryEntity : categoryEntities) {
            Long catId = categoryEntity.getCatId();
            QueryWrapper<CategoryEntity> w = new QueryWrapper<>();
            w.eq("parent_cid",catId);
            List<CategoryEntity> l1 = baseMapper.selectList(w);
            categoryEntity.setChildren(l1);
            for (CategoryEntity entity : l1) {
                Long catId1 = entity.getCatId();
                QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
                w2.eq("parent_cid",catId1);
                List<CategoryEntity> l2 = baseMapper.selectList(w2);
                entity.setChildren(l2);
            }
        }
        return categoryEntities;
    }
*/

 /*   @Override
    public List<CategoryEntity> treeList() {
        //1、和数据库做一次交互
        List<CategoryEntity> allList = baseMapper.selectList(null);
        //2、从数据库中筛选出一级分类
        //函数式接口
        //收集完数据之后需要整合
        List<CategoryEntity> l1List = allList.stream().filter(item -> {
            return item.getParentCid().equals(0L);
        }).map(l1 -> {
            //l1对应的是  return item.getParentCid().equals(0L); 他所返回的对象
            List<CategoryEntity> children = getChildren(allList, l1);
            l1.setChildren(children);
            return l1;
        }).collect(Collectors.toList());
        return l1List;
    }
    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity cate) {
        List<CategoryEntity> childrens = allList.stream().filter(item -> {
            return item.getParentCid().equals(cate.getCatId());
        }).map(c -> {
            List<CategoryEntity> children = getChildren(allList, c);
            c.setChildren(children);
            return c;
        }).collect(Collectors.toList());
        return childrens;
    }*/
    /*@Override
    public List<CategoryEntity> treeList() {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_cid",0);
        List<CategoryEntity> categoryEntities = baseMapper.selectList(wrapper);
        for (CategoryEntity categoryEntity : categoryEntities) {
            Long catId = categoryEntity.getCatId();
            QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
            w1.eq("parent_cid",catId);
            List<CategoryEntity> categoryEntities1 = baseMapper.selectList(w1);
            categoryEntity.setChildren(categoryEntities1);
            for (CategoryEntity entity : categoryEntities1) {
                Long catId1 = entity.getCatId();
                QueryWrapper<CategoryEntity> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("parent_cid",catId1);
                List<CategoryEntity> categoryEntities2 = baseMapper.selectList(wrapper1);
                entity.setChildren(categoryEntities2);
            }

        }
        return categoryEntities;
    }*/


}